package com.campus.web.controller.study;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.campus.app.service.CheckEveningsService;
import com.campus.app.service.StudentsService;
import com.campus.app.service.TeachersService;
import com.campus.commons.entity.CheckEvening;
import com.campus.commons.entity.Class;
import com.campus.commons.entity.ClassRoom;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * <p>
 * 晚自习值班表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/check-evening")
public class CheckEveningController {

    @Autowired
    private CheckEveningsService checkEveningsService;
    @Autowired
    private TeachersService teachersService;
    @Autowired
    private StudentsService studentsService;
    @GetMapping("/")
    public ModelAndView toCheckEvening() {
        return new ModelAndView("learn/night_study");
    }

    @GetMapping("/edit")
    public ModelAndView toEdit(int id) {
        ModelAndView modelAndView = Edit();
        modelAndView.addObject("edit", true);
        modelAndView.addObject("info",checkEveningsService.getOne(id));
        return modelAndView;
    }

    private ModelAndView Edit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("class", Class.classes.values());
        List<HashMap<String, Object>> classrooms = ClassRoom.classrooms.keySet().stream().map(s -> {
            HashMap<String, Object> sr = new HashMap<>();
            sr.put("classroomName", ClassRoom.classrooms.get(s));
            sr.put("id", s);
            return sr;
        }).collect(Collectors.toList());
        modelAndView.addObject("classroom",classrooms );
        modelAndView.addObject("teachers", teachersService.getTeachersByRoleId(4).getData());
        modelAndView.addObject("students", studentsService.getStudentByDeId(1).getData());
        modelAndView.setViewName("learn/night_study_Edit");
        return modelAndView;
    }
    @GetMapping("/add")
    public ModelAndView toadd() {
        ModelAndView edit = Edit();
        edit.addObject("edit", false);
             return edit;
    }

    @PostMapping("/all")
    public String getAllCheckEvening(MyPage page) {
        return JSON.toJSONString(checkEveningsService.getAllCheckEvening(page), SerializerFeature.WriteDateUseDateFormat);
    }

    @PostMapping("/search")
    public String getCheckEvening(MyPage<CheckEvening> page, CheckEvening checkEvening,Date start, Date end) {
        page.setData(checkEvening);
        return JSON.toJSONString(checkEveningsService.getCheckEvenings(page, start, end), SerializerFeature.WriteDateUseDateFormat);
    }

    @PostMapping("/add")
    public String addCheckEvening(CheckEvening checkEvening) {
        checkEvening.setIsred(checkEvening.getIsred()==null?0:1);
        return JSON.toJSONString(checkEveningsService.addCheckEvening(checkEvening));
    }

    @PostMapping("/del")
    public String delCheckEvening(int id) {
        return JSON.toJSONString(checkEveningsService.delCheckEvening(id));
    }

    @PostMapping("/update")
    public String upCheckEvening(CheckEvening checkEvening) {
        return JSON.toJSONString(checkEveningsService.upCheckEvening(checkEvening));
    }


    //<editor-fold desc="查班缓存">
    @ApiOperation("添加晚自习检查缓存信息")
    @PostMapping("/addCache")
    public Result addCacheOfCheck(CheckEvening checkEvening) {
        return checkEveningsService.addCheckEveningMap(checkEvening);
    }
    @PostMapping("/delCache")
    @ApiOperation("删除缓存信息根据classId班级Id")
    public Result delCacheOfCheck(Integer id) {
        return checkEveningsService.delCheckEventingMap(id);
    }
    @PostMapping("/getCache")
@ApiOperation("获取检查的缓存信息")
    public Result getCacheOfCheck() {
        return checkEveningsService.getCacheMap();
    }
    @PostMapping("/upCache")
    @ApiOperation("更新缓存信息")
    public Result updateCacheOfCheck(CheckEvening checkEvening) {
        return checkEveningsService.upCheckEventingMap(checkEvening);
    }
    //</editor-fold>

}

